package cn.com.nes.site.service.crm.customer.impl;

import cn.com.nes.mybatis.agent.crm.entity.CustomerHouseRelationship;
import cn.com.nes.mybatis.agent.crm.mapper.CustomerHouseRelationshipMapper;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.entity.vo.crm.CustomerHouseRelationshipVO;
import cn.com.nes.site.service.crm.customer.CustomerHouseRelationshipService;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 客户房屋关系实现
 */
@Service
public class CustomerHouseRelationshipServiceImpl extends ServiceImpl<CustomerHouseRelationshipMapper, CustomerHouseRelationship> implements CustomerHouseRelationshipService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustomerHouseRelationship saveRelationship(CustomerHouseRelationshipVO vo) {
        CustomerHouseRelationship customerHouseRelationship = new CustomerHouseRelationship();
        BeanUtils.copyProperties(vo, customerHouseRelationship);

        if (Objects.isNull(customerHouseRelationship.getId())) {
            customerHouseRelationship.setGmtCreate(new Date());
            customerHouseRelationship.setGmtCreateId(Long.valueOf(vo.getUserInfo().getId()));
        }
        customerHouseRelationship.setDeleted(0); // 0 - 没删除
        customerHouseRelationship.setGmtModified(new Date());
        customerHouseRelationship.setGmtModifiedId(Long.valueOf(vo.getUserInfo().getId()));

        this.saveOrUpdate(customerHouseRelationship);

        return customerHouseRelationship;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delRelationship(Long houseId) throws Exception {
        LambdaQueryWrapper<CustomerHouseRelationship> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerHouseRelationship::getHouseId, houseId);
        wrapper.eq(CustomerHouseRelationship::getDeleted, DeleteEnum._0.getKey());
        List<CustomerHouseRelationship> customerHouseRelationships = this.baseMapper.selectList(wrapper);
        if (CollectionUtil.isEmpty(customerHouseRelationships)) {
            return;
        }
        for (CustomerHouseRelationship customerHouseRelationship : customerHouseRelationships) {
            customerHouseRelationship.setDeleted(1);
            Integer rows = this.baseMapper.updateById(customerHouseRelationship);
            if (1 != rows) {
                throw new Exception("删除客户房屋信息失败");
            }
        }
    }

    @Override
    public CustomerHouseRelationship getCustomerHouseRelationship(String customerUuid, Long houseId) {
        LambdaQueryWrapper<CustomerHouseRelationship> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerHouseRelationship::getDeleted, DeleteEnum._0.getKey());
        wrapper.eq(CustomerHouseRelationship::getCustomerId, customerUuid);
        wrapper.eq(CustomerHouseRelationship::getHouseId, houseId);
        return this.getOne(wrapper);
    }

    @Override
    public CustomerHouseRelationship getCustomerHouseId(String customerUuid) {
        LambdaQueryWrapper<CustomerHouseRelationship> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerHouseRelationship::getDeleted, DeleteEnum._0.getKey());
        wrapper.eq(CustomerHouseRelationship::getCustomerId, customerUuid);
        return this.getOne(wrapper);
    }

}
